{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from unicodedata import normalize\n",
    "from tensorflow import keras\n",
    "import os\n",
    "from tensorflow.keras.models import Sequential, load_model\n",
    "from tensorflow.keras.optimizers import RMSprop\n",
    "from collections import Counter\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from matplotlib import pyplot as plt\n",
    "from tensorflow.keras.layers import Conv2D, ZeroPadding2D, AveragePooling2D, BatchNormalization, Activation, Dense, \\\n",
    "    Input, MaxPooling2D,Flatten,Dropout\n",
    "from tensorflow.keras import Model, layers, regularizers\n",
    "import tensorflow.keras.backend as K\n",
    "from tensorflow.keras.datasets import cifar10\n",
    "from tensorflow.keras.callbacks import LearningRateScheduler, ModelCheckpoint, EarlyStopping, ReduceLROnPlateau\n",
    "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
    "from tensorflow.keras.applications.resnet50 import preprocess_input\n",
    "from tensorflow.keras.initializers import he_normal\n",
    "from tensorflow.keras import optimizers\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn import preprocessing\n",
    "from sklearn.metrics import accuracy_score\n",
    "from tqdm import tqdm\n",
    "from tensorflow.keras.models import load_model\n",
    "import csv\n",
    "import cv2\n",
    "#importing model \n",
    "from tensorflow.keras.applications import ResNet50, densenet,VGG16"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = csv.reader(open('../../data/data_set/Car_label.csv','r',encoding='utf-8'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_class = 49"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_x = []\n",
    "test_x = []\n",
    "train_y = []\n",
    "test_y = []\n",
    "X_data = []\n",
    "Y_data = []\n",
    "input_shape=(96,96,3)#3通道图像数据\n",
    "for lists in f:\n",
    "    img = cv2.imread('../../data/data_set/'+lists[1])\n",
    "    img = cv2.resize(img, (input_shape[0], input_shape[1]))\n",
    "    X_data.append(img)\n",
    "    Y_data.append(int(lists[2]))\n",
    "#     if lists[3] == '1':\n",
    "#         train_x.append(img)\n",
    "#         train_y.append(int(lists[2]))\n",
    "#     else:\n",
    "#         test_x.append(img)\n",
    "#         test_y.append(int(lists[2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_x,test_x,train_y,test_y = train_test_split(X_data,Y_data,test_size=0.1)\n",
    "train_x = np.array(train_x).astype('float32') / 255.\n",
    "train_y = np.array(train_y)\n",
    "test_x = np.array(test_x).astype('float32') / 255.\n",
    "test_y = np.array(test_y)\n",
    "# train_x_mean = np.mean(train_x, axis=0)\n",
    "# test_x_mean = np.mean(test_x, axis=0)\n",
    "# train_x -= train_x_mean\n",
    "# test_x -= test_x_mean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_x = np.array(train_x)\n",
    "train_y = np.array(train_y)\n",
    "test_x = np.array(test_x)\n",
    "test_y = np.array(test_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "lb = preprocessing.LabelBinarizer().fit(np.array(range(num_class)))  # 对标签进行ont_hot编码\n",
    "train_y = lb.transform(train_y)  # 因为是多分类任务，必须进行编码处理\n",
    "test_y = lb.transform(test_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def build_model_with_resnet50(freeze_wights=False):\n",
    "\n",
    "    Backbone = ResNet50(\n",
    "    include_top=False, weights='imagenet', pooling='avg')\n",
    "\n",
    "    if freeze_wights:\n",
    "        Backbone.trainable = False\n",
    "        for layer in Backbone.layers:\n",
    "            if \"BatchNormalization\" in layer.__class__.__name__:\n",
    "                layer.trainable = True\n",
    "            else:\n",
    "                layer.trainable = False\n",
    "    else:\n",
    "        Backbone.trainable = True\n",
    "\n",
    "    X = BatchNormalization()(Backbone.output)\n",
    "\n",
    "    X = Flatten(name=\"flatten\")(X)\n",
    "    \n",
    "    X = Dense(1024)(X)\n",
    "    X = Activation('relu')(X) \n",
    "\n",
    "    X = BatchNormalization()(X)\n",
    "\n",
    "    X = Dense(num_class)(X)\n",
    "    X = Activation('softmax')(X)\n",
    "\n",
    "    model = Model(Backbone.input, X)\n",
    "\n",
    "    return model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def build_model_with_denseNet121(freeze_wights=False):\n",
    "\n",
    "    Backbone = densenet.DenseNet121(\n",
    "    include_top=False, weights='imagenet', pooling='avg')\n",
    "\n",
    "    if freeze_wights:\n",
    "        Backbone.trainable = False\n",
    "        for layer in Backbone.layers:\n",
    "            if \"BatchNormalization\" in layer.__class__.__name__:\n",
    "                layer.trainable = True\n",
    "            else:\n",
    "                layer.trainable = False\n",
    "    else:\n",
    "        Backbone.trainable = True\n",
    "\n",
    "    X = BatchNormalization()(Backbone.output)\n",
    "\n",
    "    X = Flatten(name=\"flatten\")(X)\n",
    "    \n",
    "    X = Dense(1024)(X)\n",
    "    X = Activation('relu')(X) \n",
    "\n",
    "    X = BatchNormalization()(X)\n",
    "\n",
    "    X = Dense(num_class)(X)\n",
    "    X = Activation('softmax')(X)\n",
    "\n",
    "    model = Model(Backbone.input, X)\n",
    "\n",
    "    return model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "ResNet_model = build_model_with_denseNet121()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "loss = 'categorical_crossentropy'\n",
    "optimizer = optimizers.Adam(lr=1e-4)\n",
    "ResNet_model.compile(loss=loss , optimizer= optimizer, metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# sgd = optimizers.SGD(lr=0.01, decay=1e-4, momentum=0.9, nesterov=True)\n",
    "optimizer = optimizers.Adam(lr=1e-4)\n",
    "ResNet_model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['acc'])\n",
    "\n",
    "# change_lr = LearningRateScheduler(scheduler)\n",
    "reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=6, verbose=1, min_delta=1e-6)\n",
    "\n",
    "save_dir = os.path.join(os.getcwd(), '../../data/trained_model')\n",
    "model_name = 'denseNet121_model.{epoch:02d}-{val_acc:.2f}.h5'\n",
    "if not os.path.isdir(save_dir):\n",
    "    os.makedirs(save_dir)\n",
    "filepath = os.path.join(save_dir, model_name)\n",
    "\n",
    "checkpoint = ModelCheckpoint(filepath=filepath,\n",
    "                             monitor='val_acc',\n",
    "                             verbose=1,\n",
    "                             save_best_only=True)\n",
    "\n",
    "#creating early stopping to prevent model from overfitting \n",
    "early_stopping = EarlyStopping(monitor=\"val_acc\", min_delta=0,\n",
    "                                                  patience=16, verbose=1, \n",
    "                                                  mode=\"auto\", baseline=None, \n",
    "                                                  restore_best_weights=True)\n",
    "\n",
    "cbks = [early_stopping, checkpoint, reduce_lr]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_data_gen = ImageDataGenerator(rotation_range=40,\n",
    "    zoom_range=0.2,\n",
    "    width_shift_range=0.2,\n",
    "    height_shift_range=0.2,\n",
    "    shear_range=0.2,\n",
    "    horizontal_flip=True,\n",
    "    fill_mode=\"nearest\")\n",
    "valid_data_gen = ImageDataGenerator()\n",
    "train_data_gen.fit(train_x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/200\n",
      "  2/251 [..............................] - ETA: 10s - loss: 4.7342 - acc: 0.0156   WARNING:tensorflow:Callbacks method `on_train_batch_end` is slow compared to the batch time (batch time: 0.0329s vs `on_train_batch_end` time: 0.0519s). Check your callbacks.\n",
      "251/251 [==============================] - ETA: 0s - loss: 4.0820 - acc: 0.0993\n",
      "Epoch 00001: val_acc improved from -inf to 0.02656, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\resnet50_model.01-0.03.h5\n",
      "251/251 [==============================] - 25s 100ms/step - loss: 4.0820 - acc: 0.0993 - val_loss: 5.3363 - val_acc: 0.0266\n",
      "Epoch 2/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 3.3242 - acc: 0.1965\n",
      "Epoch 00002: val_acc improved from 0.02656 to 0.11365, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\resnet50_model.02-0.11.h5\n",
      "251/251 [==============================] - 24s 95ms/step - loss: 3.3242 - acc: 0.1965 - val_loss: 4.1773 - val_acc: 0.1137\n",
      "Epoch 3/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 2.9336 - acc: 0.2624\n",
      "Epoch 00003: val_acc did not improve from 0.11365\n",
      "251/251 [==============================] - 23s 92ms/step - loss: 2.9336 - acc: 0.2624 - val_loss: 3.8679 - val_acc: 0.0926\n",
      "Epoch 4/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 2.5816 - acc: 0.3144\n",
      "Epoch 00004: val_acc improved from 0.11365 to 0.26189, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\resnet50_model.04-0.26.h5\n",
      "251/251 [==============================] - 24s 95ms/step - loss: 2.5816 - acc: 0.3144 - val_loss: 3.3519 - val_acc: 0.2619\n",
      "Epoch 5/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 2.3381 - acc: 0.3658\n",
      "Epoch 00005: val_acc improved from 0.26189 to 0.32489, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\resnet50_model.05-0.32.h5\n",
      "251/251 [==============================] - 24s 96ms/step - loss: 2.3381 - acc: 0.3658 - val_loss: 2.7307 - val_acc: 0.3249\n",
      "Epoch 6/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 2.1479 - acc: 0.4137\n",
      "Epoch 00006: val_acc improved from 0.32489 to 0.39531, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\resnet50_model.06-0.40.h5\n",
      "251/251 [==============================] - 24s 96ms/step - loss: 2.1479 - acc: 0.4137 - val_loss: 2.3410 - val_acc: 0.3953\n",
      "Epoch 7/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.9694 - acc: 0.4468\n",
      "Epoch 00007: val_acc did not improve from 0.39531\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 1.9694 - acc: 0.4468 - val_loss: 2.6717 - val_acc: 0.3891\n",
      "Epoch 8/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.7868 - acc: 0.4892\n",
      "Epoch 00008: val_acc improved from 0.39531 to 0.44719, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\resnet50_model.08-0.45.h5\n",
      "251/251 [==============================] - 24s 96ms/step - loss: 1.7868 - acc: 0.4892 - val_loss: 2.1586 - val_acc: 0.4472\n",
      "Epoch 9/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.7111 - acc: 0.5129\n",
      "Epoch 00009: val_acc did not improve from 0.44719\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 1.7111 - acc: 0.5129 - val_loss: 2.2618 - val_acc: 0.4194\n",
      "Epoch 10/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.5948 - acc: 0.5410\n",
      "Epoch 00010: val_acc improved from 0.44719 to 0.45769, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\resnet50_model.10-0.46.h5\n",
      "251/251 [==============================] - 24s 96ms/step - loss: 1.5948 - acc: 0.5410 - val_loss: 2.0095 - val_acc: 0.4577\n",
      "Epoch 11/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.4617 - acc: 0.5708- ETA:  - ETA: 0s - loss: 1.4642 - acc\n",
      "Epoch 00011: val_acc did not improve from 0.45769\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 1.4617 - acc: 0.5708 - val_loss: 2.1516 - val_acc: 0.4472\n",
      "Epoch 12/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.3761 - acc: 0.5919\n",
      "Epoch 00012: val_acc improved from 0.45769 to 0.47498, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\resnet50_model.12-0.47.h5\n",
      "251/251 [==============================] - 24s 96ms/step - loss: 1.3761 - acc: 0.5919 - val_loss: 2.1066 - val_acc: 0.4750\n",
      "Epoch 13/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.3513 - acc: 0.6082\n",
      "Epoch 00013: val_acc did not improve from 0.47498\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 1.3513 - acc: 0.6082 - val_loss: 2.4524 - val_acc: 0.4379\n",
      "Epoch 14/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.2346 - acc: 0.6320\n",
      "Epoch 00014: val_acc improved from 0.47498 to 0.52625, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\resnet50_model.14-0.53.h5\n",
      "251/251 [==============================] - 24s 96ms/step - loss: 1.2346 - acc: 0.6320 - val_loss: 1.8273 - val_acc: 0.5263\n",
      "Epoch 15/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.1624 - acc: 0.6559\n",
      "Epoch 00015: val_acc improved from 0.52625 to 0.54169, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\resnet50_model.15-0.54.h5\n",
      "251/251 [==============================] - 24s 96ms/step - loss: 1.1624 - acc: 0.6559 - val_loss: 1.8030 - val_acc: 0.5417\n",
      "Epoch 16/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.0807 - acc: 0.6804\n",
      "Epoch 00016: val_acc did not improve from 0.54169\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 1.0807 - acc: 0.6804 - val_loss: 2.0350 - val_acc: 0.5108\n",
      "Epoch 17/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.0848 - acc: 0.6765\n",
      "Epoch 00017: val_acc improved from 0.54169 to 0.56455, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\resnet50_model.17-0.56.h5\n",
      "251/251 [==============================] - 24s 96ms/step - loss: 1.0848 - acc: 0.6765 - val_loss: 1.7604 - val_acc: 0.5645\n",
      "Epoch 18/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.9806 - acc: 0.7009- ETA: 6\n",
      "Epoch 00018: val_acc did not improve from 0.56455\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.9806 - acc: 0.7009 - val_loss: 1.8536 - val_acc: 0.5368\n",
      "Epoch 19/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.9462 - acc: 0.7135\n",
      "Epoch 00019: val_acc did not improve from 0.56455\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 0.9462 - acc: 0.7135 - val_loss: 1.7894 - val_acc: 0.5584\n",
      "Epoch 20/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.8802 - acc: 0.7316\n",
      "Epoch 00020: val_acc did not improve from 0.56455\n",
      "251/251 [==============================] - 23s 94ms/step - loss: 0.8802 - acc: 0.7316 - val_loss: 1.8330 - val_acc: 0.5485\n",
      "Epoch 21/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.8573 - acc: 0.7448\n",
      "Epoch 00021: val_acc did not improve from 0.56455\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.8573 - acc: 0.7448 - val_loss: 2.0255 - val_acc: 0.5275\n",
      "Epoch 22/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.8180 - acc: 0.7527- ETA: 0s - loss: 0.8186 - acc: 0.7 - ETA: 0s - loss: 0.8190 - acc: 0.7525\n",
      "Epoch 00022: val_acc improved from 0.56455 to 0.59852, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\resnet50_model.22-0.60.h5\n",
      "251/251 [==============================] - 24s 96ms/step - loss: 0.8190 - acc: 0.7525 - val_loss: 1.5739 - val_acc: 0.5985\n",
      "Epoch 23/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.7627 - acc: 0.7622\n",
      "Epoch 00023: val_acc did not improve from 0.59852\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.7627 - acc: 0.7622 - val_loss: 1.6643 - val_acc: 0.5905\n",
      "Epoch 24/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.7412 - acc: 0.7739- ETA: 1s - loss: 0.7398\n",
      "Epoch 00024: val_acc did not improve from 0.59852\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 0.7412 - acc: 0.7739 - val_loss: 1.8427 - val_acc: 0.5670\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 25/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.7305 - acc: 0.7744\n",
      "Epoch 00025: val_acc improved from 0.59852 to 0.60655, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\resnet50_model.25-0.61.h5\n",
      "251/251 [==============================] - 24s 96ms/step - loss: 0.7305 - acc: 0.7744 - val_loss: 1.6660 - val_acc: 0.6065\n",
      "Epoch 26/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.7296 - acc: 0.7793\n",
      "Epoch 00026: val_acc did not improve from 0.60655\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.7296 - acc: 0.7793 - val_loss: 1.9222 - val_acc: 0.5553\n",
      "Epoch 27/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.7337 - acc: 0.7724- ETA: 0s - loss: 0.7308 - acc: 0.\n",
      "Epoch 00027: val_acc did not improve from 0.60655\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.7337 - acc: 0.7724 - val_loss: 1.6309 - val_acc: 0.5991\n",
      "Epoch 28/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.6386 - acc: 0.8025\n",
      "Epoch 00028: val_acc did not improve from 0.60655\n",
      "\n",
      "Epoch 00028: ReduceLROnPlateau reducing learning rate to 9.999999747378752e-06.\n",
      "251/251 [==============================] - 23s 94ms/step - loss: 0.6386 - acc: 0.8025 - val_loss: 1.7525 - val_acc: 0.6022\n",
      "Epoch 29/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.5162 - acc: 0.8366\n",
      "Epoch 00029: val_acc improved from 0.60655 to 0.65596, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\resnet50_model.29-0.66.h5\n",
      "251/251 [==============================] - 24s 96ms/step - loss: 0.5162 - acc: 0.8366 - val_loss: 1.3991 - val_acc: 0.6560\n",
      "Epoch 30/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.4642 - acc: 0.8616- ETA: 1s - loss: 0.4649 - acc:  - ETA: 1s - loss: 0.4650\n",
      "Epoch 00030: val_acc improved from 0.65596 to 0.67881, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\resnet50_model.30-0.68.h5\n",
      "251/251 [==============================] - 24s 96ms/step - loss: 0.4642 - acc: 0.8616 - val_loss: 1.3321 - val_acc: 0.6788\n",
      "Epoch 31/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.4243 - acc: 0.8730- ETA: 0s - loss: 0.4259 - acc: 0.\n",
      "Epoch 00031: val_acc did not improve from 0.67881\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.4243 - acc: 0.8730 - val_loss: 1.3231 - val_acc: 0.6763\n",
      "Epoch 32/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.3971 - acc: 0.8792- ETA: 2s\n",
      "Epoch 00032: val_acc improved from 0.67881 to 0.68067, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\resnet50_model.32-0.68.h5\n",
      "251/251 [==============================] - 24s 96ms/step - loss: 0.3971 - acc: 0.8792 - val_loss: 1.3067 - val_acc: 0.6807\n",
      "Epoch 33/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.3718 - acc: 0.8910\n",
      "Epoch 00033: val_acc improved from 0.68067 to 0.68499, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\resnet50_model.33-0.68.h5\n",
      "251/251 [==============================] - 24s 96ms/step - loss: 0.3718 - acc: 0.8910 - val_loss: 1.3219 - val_acc: 0.6850\n",
      "Epoch 34/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.3639 - acc: 0.8883\n",
      "Epoch 00034: val_acc improved from 0.68499 to 0.69426, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\resnet50_model.34-0.69.h5\n",
      "251/251 [==============================] - 24s 97ms/step - loss: 0.3639 - acc: 0.8883 - val_loss: 1.3054 - val_acc: 0.6943\n",
      "Epoch 35/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.3542 - acc: 0.8943\n",
      "Epoch 00035: val_acc did not improve from 0.69426\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.3542 - acc: 0.8943 - val_loss: 1.3318 - val_acc: 0.6875\n",
      "Epoch 36/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.3300 - acc: 0.9039\n",
      "Epoch 00036: val_acc did not improve from 0.69426\n",
      "251/251 [==============================] - 23s 94ms/step - loss: 0.3300 - acc: 0.9039 - val_loss: 1.3364 - val_acc: 0.6862\n",
      "Epoch 37/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.3314 - acc: 0.9004\n",
      "Epoch 00037: val_acc improved from 0.69426 to 0.69920, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\resnet50_model.37-0.70.h5\n",
      "251/251 [==============================] - 24s 96ms/step - loss: 0.3314 - acc: 0.9004 - val_loss: 1.2688 - val_acc: 0.6992\n",
      "Epoch 38/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.3161 - acc: 0.9058- ETA: 1s - loss: 0.\n",
      "Epoch 00038: val_acc did not improve from 0.69920\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.3161 - acc: 0.9058 - val_loss: 1.3136 - val_acc: 0.6930\n",
      "Epoch 39/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.3012 - acc: 0.9107\n",
      "Epoch 00039: val_acc improved from 0.69920 to 0.70599, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\resnet50_model.39-0.71.h5\n",
      "251/251 [==============================] - 24s 95ms/step - loss: 0.3012 - acc: 0.9107 - val_loss: 1.2691 - val_acc: 0.7060\n",
      "Epoch 40/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2859 - acc: 0.9156\n",
      "Epoch 00040: val_acc did not improve from 0.70599\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2859 - acc: 0.9156 - val_loss: 1.2952 - val_acc: 0.6936\n",
      "Epoch 41/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2932 - acc: 0.9120- ETA: 4s - loss: 0 - ETA: 2s \n",
      "Epoch 00041: val_acc did not improve from 0.70599\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2932 - acc: 0.9120 - val_loss: 1.2825 - val_acc: 0.6986\n",
      "Epoch 42/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2796 - acc: 0.9203\n",
      "Epoch 00042: val_acc did not improve from 0.70599\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2796 - acc: 0.9203 - val_loss: 1.3000 - val_acc: 0.6936\n",
      "Epoch 43/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2647 - acc: 0.9212\n",
      "Epoch 00043: val_acc did not improve from 0.70599\n",
      "\n",
      "Epoch 00043: ReduceLROnPlateau reducing learning rate to 9.999999747378752e-07.\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2647 - acc: 0.9212 - val_loss: 1.2888 - val_acc: 0.6955\n",
      "Epoch 44/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2511 - acc: 0.9255\n",
      "Epoch 00044: val_acc did not improve from 0.70599\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2511 - acc: 0.9255 - val_loss: 1.2702 - val_acc: 0.6998\n",
      "Epoch 45/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2598 - acc: 0.9258\n",
      "Epoch 00045: val_acc did not improve from 0.70599\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2598 - acc: 0.9258 - val_loss: 1.2710 - val_acc: 0.6998\n",
      "Epoch 46/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2668 - acc: 0.9186- ETA: 4s - loss: 0.2\n",
      "Epoch 00046: val_acc did not improve from 0.70599\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2668 - acc: 0.9186 - val_loss: 1.2693 - val_acc: 0.6998\n",
      "Epoch 47/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2478 - acc: 0.9273\n",
      "Epoch 00047: val_acc did not improve from 0.70599\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2478 - acc: 0.9273 - val_loss: 1.2723 - val_acc: 0.7017\n",
      "Epoch 48/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2578 - acc: 0.9232\n",
      "Epoch 00048: val_acc did not improve from 0.70599\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2578 - acc: 0.9232 - val_loss: 1.2657 - val_acc: 0.7029\n",
      "Epoch 49/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2514 - acc: 0.9268\n",
      "Epoch 00049: val_acc did not improve from 0.70599\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2514 - acc: 0.9268 - val_loss: 1.2623 - val_acc: 0.7023\n",
      "Epoch 50/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2450 - acc: 0.9289\n",
      "Epoch 00050: val_acc did not improve from 0.70599\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2450 - acc: 0.9289 - val_loss: 1.2647 - val_acc: 0.7041\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 51/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2478 - acc: 0.9287\n",
      "Epoch 00051: val_acc improved from 0.70599 to 0.70723, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\resnet50_model.51-0.71.h5\n",
      "251/251 [==============================] - 24s 95ms/step - loss: 0.2478 - acc: 0.9287 - val_loss: 1.2593 - val_acc: 0.7072\n",
      "Epoch 52/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2410 - acc: 0.9294\n",
      "Epoch 00052: val_acc did not improve from 0.70723\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2410 - acc: 0.9294 - val_loss: 1.2609 - val_acc: 0.7035\n",
      "Epoch 53/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2305 - acc: 0.9333\n",
      "Epoch 00053: val_acc did not improve from 0.70723\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2305 - acc: 0.9333 - val_loss: 1.2521 - val_acc: 0.7048\n",
      "Epoch 54/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2492 - acc: 0.9292\n",
      "Epoch 00054: val_acc did not improve from 0.70723\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2492 - acc: 0.9292 - val_loss: 1.2525 - val_acc: 0.7060\n",
      "Epoch 55/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2493 - acc: 0.9259- ETA: 0s - loss: 0.2492 - ac\n",
      "Epoch 00055: val_acc did not improve from 0.70723\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2493 - acc: 0.9259 - val_loss: 1.2627 - val_acc: 0.7060\n",
      "Epoch 56/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2422 - acc: 0.9340\n",
      "Epoch 00056: val_acc did not improve from 0.70723\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2422 - acc: 0.9340 - val_loss: 1.2552 - val_acc: 0.7054\n",
      "Epoch 57/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2386 - acc: 0.9309\n",
      "Epoch 00057: val_acc did not improve from 0.70723\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2386 - acc: 0.9309 - val_loss: 1.2478 - val_acc: 0.7072\n",
      "Epoch 58/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2434 - acc: 0.9295\n",
      "Epoch 00058: val_acc improved from 0.70723 to 0.70784, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\resnet50_model.58-0.71.h5\n",
      "251/251 [==============================] - 24s 95ms/step - loss: 0.2434 - acc: 0.9295 - val_loss: 1.2498 - val_acc: 0.7078\n",
      "Epoch 59/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2348 - acc: 0.9324\n",
      "Epoch 00059: val_acc did not improve from 0.70784\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2348 - acc: 0.9324 - val_loss: 1.2533 - val_acc: 0.7035\n",
      "Epoch 60/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2342 - acc: 0.9308\n",
      "Epoch 00060: val_acc did not improve from 0.70784\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2342 - acc: 0.9308 - val_loss: 1.2476 - val_acc: 0.7072\n",
      "Epoch 61/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2430 - acc: 0.9258\n",
      "Epoch 00061: val_acc improved from 0.70784 to 0.70846, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\resnet50_model.61-0.71.h5\n",
      "251/251 [==============================] - 24s 95ms/step - loss: 0.2430 - acc: 0.9258 - val_loss: 1.2428 - val_acc: 0.7085\n",
      "Epoch 62/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2339 - acc: 0.9322\n",
      "Epoch 00062: val_acc did not improve from 0.70846\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2339 - acc: 0.9322 - val_loss: 1.2413 - val_acc: 0.7060\n",
      "Epoch 63/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2206 - acc: 0.9368- ETA: 0s - loss: 0.2208 - \n",
      "Epoch 00063: val_acc did not improve from 0.70846\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 0.2206 - acc: 0.9368 - val_loss: 1.2429 - val_acc: 0.7085\n",
      "Epoch 64/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2387 - acc: 0.9299\n",
      "Epoch 00064: val_acc did not improve from 0.70846\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2387 - acc: 0.9299 - val_loss: 1.2458 - val_acc: 0.7072\n",
      "Epoch 65/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2403 - acc: 0.9292\n",
      "Epoch 00065: val_acc did not improve from 0.70846\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2403 - acc: 0.9292 - val_loss: 1.2379 - val_acc: 0.7060\n",
      "Epoch 66/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2207 - acc: 0.9377\n",
      "Epoch 00066: val_acc did not improve from 0.70846\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2207 - acc: 0.9377 - val_loss: 1.2467 - val_acc: 0.7078\n",
      "Epoch 67/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2209 - acc: 0.9338- ETA:\n",
      "Epoch 00067: val_acc improved from 0.70846 to 0.70908, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\resnet50_model.67-0.71.h5\n",
      "251/251 [==============================] - 24s 96ms/step - loss: 0.2209 - acc: 0.9338 - val_loss: 1.2448 - val_acc: 0.7091\n",
      "Epoch 68/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2316 - acc: 0.9331\n",
      "Epoch 00068: val_acc did not improve from 0.70908\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2316 - acc: 0.9331 - val_loss: 1.2407 - val_acc: 0.7060\n",
      "Epoch 69/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2177 - acc: 0.9375\n",
      "Epoch 00069: val_acc did not improve from 0.70908\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2177 - acc: 0.9375 - val_loss: 1.2371 - val_acc: 0.7072\n",
      "Epoch 70/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2303 - acc: 0.9324\n",
      "Epoch 00070: val_acc did not improve from 0.70908\n",
      "251/251 [==============================] - 23s 94ms/step - loss: 0.2303 - acc: 0.9324 - val_loss: 1.2503 - val_acc: 0.7072\n",
      "Epoch 71/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2213 - acc: 0.9365\n",
      "Epoch 00071: val_acc did not improve from 0.70908\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2213 - acc: 0.9365 - val_loss: 1.2418 - val_acc: 0.7091\n",
      "Epoch 72/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2181 - acc: 0.9412\n",
      "Epoch 00072: val_acc improved from 0.70908 to 0.70970, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\resnet50_model.72-0.71.h5\n",
      "251/251 [==============================] - 24s 95ms/step - loss: 0.2181 - acc: 0.9412 - val_loss: 1.2401 - val_acc: 0.7097\n",
      "Epoch 73/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2321 - acc: 0.9313- ETA:\n",
      "Epoch 00073: val_acc did not improve from 0.70970\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2321 - acc: 0.9313 - val_loss: 1.2397 - val_acc: 0.7091\n",
      "Epoch 74/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2241 - acc: 0.9349- ETA\n",
      "Epoch 00074: val_acc improved from 0.70970 to 0.71093, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\resnet50_model.74-0.71.h5\n",
      "251/251 [==============================] - 24s 95ms/step - loss: 0.2241 - acc: 0.9349 - val_loss: 1.2418 - val_acc: 0.7109\n",
      "Epoch 75/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2276 - acc: 0.9333\n",
      "Epoch 00075: val_acc did not improve from 0.71093\n",
      "251/251 [==============================] - 23s 92ms/step - loss: 0.2276 - acc: 0.9333 - val_loss: 1.2361 - val_acc: 0.7078\n",
      "Epoch 76/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2283 - acc: 0.9317\n",
      "Epoch 00076: val_acc improved from 0.71093 to 0.71155, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\resnet50_model.76-0.71.h5\n",
      "251/251 [==============================] - 24s 96ms/step - loss: 0.2283 - acc: 0.9317 - val_loss: 1.2388 - val_acc: 0.7116\n",
      "Epoch 77/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2195 - acc: 0.9373\n",
      "Epoch 00077: val_acc improved from 0.71155 to 0.71340, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\resnet50_model.77-0.71.h5\n",
      "251/251 [==============================] - 24s 95ms/step - loss: 0.2195 - acc: 0.9373 - val_loss: 1.2409 - val_acc: 0.7134\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 78/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2254 - acc: 0.9343\n",
      "Epoch 00078: val_acc did not improve from 0.71340\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2254 - acc: 0.9343 - val_loss: 1.2335 - val_acc: 0.7122\n",
      "Epoch 79/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2281 - acc: 0.9357\n",
      "Epoch 00079: val_acc did not improve from 0.71340\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2281 - acc: 0.9357 - val_loss: 1.2410 - val_acc: 0.7134\n",
      "Epoch 80/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2145 - acc: 0.9385\n",
      "Epoch 00080: val_acc did not improve from 0.71340\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2145 - acc: 0.9385 - val_loss: 1.2468 - val_acc: 0.7078\n",
      "Epoch 81/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2271 - acc: 0.9320\n",
      "Epoch 00081: val_acc did not improve from 0.71340\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2271 - acc: 0.9320 - val_loss: 1.2464 - val_acc: 0.7078\n",
      "Epoch 82/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2139 - acc: 0.9369\n",
      "Epoch 00082: val_acc did not improve from 0.71340\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 0.2139 - acc: 0.9369 - val_loss: 1.2438 - val_acc: 0.7109\n",
      "Epoch 83/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2162 - acc: 0.9389\n",
      "Epoch 00083: val_acc did not improve from 0.71340\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2162 - acc: 0.9389 - val_loss: 1.2397 - val_acc: 0.7109\n",
      "Epoch 84/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2151 - acc: 0.9399\n",
      "Epoch 00084: val_acc did not improve from 0.71340\n",
      "\n",
      "Epoch 00084: ReduceLROnPlateau reducing learning rate to 9.999999974752428e-08.\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 0.2151 - acc: 0.9399 - val_loss: 1.2427 - val_acc: 0.7085\n",
      "Epoch 85/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2170 - acc: 0.9380\n",
      "Epoch 00085: val_acc did not improve from 0.71340\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 0.2170 - acc: 0.9380 - val_loss: 1.2392 - val_acc: 0.7097\n",
      "Epoch 86/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2121 - acc: 0.9394\n",
      "Epoch 00086: val_acc did not improve from 0.71340\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2121 - acc: 0.9394 - val_loss: 1.2370 - val_acc: 0.7116\n",
      "Epoch 87/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2203 - acc: 0.9360\n",
      "Epoch 00087: val_acc did not improve from 0.71340\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2203 - acc: 0.9360 - val_loss: 1.2394 - val_acc: 0.7122\n",
      "Epoch 88/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2134 - acc: 0.9384\n",
      "Epoch 00088: val_acc did not improve from 0.71340\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2134 - acc: 0.9384 - val_loss: 1.2520 - val_acc: 0.7134\n",
      "Epoch 89/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2138 - acc: 0.9374- ETA: 0s - loss: 0.2142 - acc: 0.937\n",
      "Epoch 00089: val_acc did not improve from 0.71340\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2138 - acc: 0.9374 - val_loss: 1.2374 - val_acc: 0.7116\n",
      "Epoch 90/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2174 - acc: 0.9377\n",
      "Epoch 00090: val_acc did not improve from 0.71340\n",
      "\n",
      "Epoch 00090: ReduceLROnPlateau reducing learning rate to 1.0000000116860975e-08.\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2174 - acc: 0.9377 - val_loss: 1.2435 - val_acc: 0.7109\n",
      "Epoch 91/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2171 - acc: 0.9397\n",
      "Epoch 00091: val_acc did not improve from 0.71340\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.2171 - acc: 0.9397 - val_loss: 1.2402 - val_acc: 0.7122\n",
      "Epoch 92/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2218 - acc: 0.9337\n",
      "Epoch 00092: val_acc did not improve from 0.71340\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 0.2218 - acc: 0.9337 - val_loss: 1.2348 - val_acc: 0.7085\n",
      "Epoch 93/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.2164 - acc: 0.9364Restoring model weights from the end of the best epoch.\n",
      "\n",
      "Epoch 00093: val_acc did not improve from 0.71340\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 0.2164 - acc: 0.9364 - val_loss: 1.2456 - val_acc: 0.7091\n",
      "Epoch 00093: early stopping\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tensorflow.python.keras.callbacks.History at 0x25fff2c2d68>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "batch_size = 32\n",
    "epochs = 200\n",
    "iterations = 251\n",
    "ResNet_model.fit(train_data_gen.flow(train_x, train_y, batch_size=batch_size),\n",
    "                    steps_per_epoch=iterations,\n",
    "                    epochs=epochs,\n",
    "                    callbacks=cbks,\n",
    "                    validation_data=valid_data_gen.flow(test_x, test_y, batch_size=batch_size))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "def build_model_with_vgg16(freeze_wights=False):\n",
    "\n",
    "    Backbone = VGG16(\n",
    "    include_top=False, weights='imagenet', pooling='avg')\n",
    "\n",
    "    if freeze_wights:\n",
    "        Backbone.trainable = False\n",
    "        for layer in Backbone.layers:\n",
    "            if \"BatchNormalization\" in layer.__class__.__name__:\n",
    "                layer.trainable = True\n",
    "            else:\n",
    "                layer.trainable = False\n",
    "    else:\n",
    "        Backbone.trainable = True\n",
    "\n",
    "    X = BatchNormalization()(Backbone.output)\n",
    "\n",
    "    X = Flatten(name=\"flatten\")(X)\n",
    "    \n",
    "    X = Dense(1024)(X)\n",
    "    X = Activation('relu')(X) \n",
    "\n",
    "    X = BatchNormalization()(X)\n",
    "\n",
    "    X = Dense(num_class)(X)\n",
    "    X = Activation('softmax')(X)\n",
    "\n",
    "    model = Model(Backbone.input, X)\n",
    "\n",
    "    return model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"functional_9\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "input_5 (InputLayer)         [(None, None, None, 3)]   0         \n",
      "_________________________________________________________________\n",
      "block1_conv1 (Conv2D)        (None, None, None, 64)    1792      \n",
      "_________________________________________________________________\n",
      "block1_conv2 (Conv2D)        (None, None, None, 64)    36928     \n",
      "_________________________________________________________________\n",
      "block1_pool (MaxPooling2D)   (None, None, None, 64)    0         \n",
      "_________________________________________________________________\n",
      "block2_conv1 (Conv2D)        (None, None, None, 128)   73856     \n",
      "_________________________________________________________________\n",
      "block2_conv2 (Conv2D)        (None, None, None, 128)   147584    \n",
      "_________________________________________________________________\n",
      "block2_pool (MaxPooling2D)   (None, None, None, 128)   0         \n",
      "_________________________________________________________________\n",
      "block3_conv1 (Conv2D)        (None, None, None, 256)   295168    \n",
      "_________________________________________________________________\n",
      "block3_conv2 (Conv2D)        (None, None, None, 256)   590080    \n",
      "_________________________________________________________________\n",
      "block3_conv3 (Conv2D)        (None, None, None, 256)   590080    \n",
      "_________________________________________________________________\n",
      "block3_pool (MaxPooling2D)   (None, None, None, 256)   0         \n",
      "_________________________________________________________________\n",
      "block4_conv1 (Conv2D)        (None, None, None, 512)   1180160   \n",
      "_________________________________________________________________\n",
      "block4_conv2 (Conv2D)        (None, None, None, 512)   2359808   \n",
      "_________________________________________________________________\n",
      "block4_conv3 (Conv2D)        (None, None, None, 512)   2359808   \n",
      "_________________________________________________________________\n",
      "block4_pool (MaxPooling2D)   (None, None, None, 512)   0         \n",
      "_________________________________________________________________\n",
      "block5_conv1 (Conv2D)        (None, None, None, 512)   2359808   \n",
      "_________________________________________________________________\n",
      "block5_conv2 (Conv2D)        (None, None, None, 512)   2359808   \n",
      "_________________________________________________________________\n",
      "block5_conv3 (Conv2D)        (None, None, None, 512)   2359808   \n",
      "_________________________________________________________________\n",
      "block5_pool (MaxPooling2D)   (None, None, None, 512)   0         \n",
      "_________________________________________________________________\n",
      "global_average_pooling2d (Gl (None, 512)               0         \n",
      "_________________________________________________________________\n",
      "batch_normalization_8 (Batch (None, 512)               2048      \n",
      "_________________________________________________________________\n",
      "flatten (Flatten)            (None, 512)               0         \n",
      "_________________________________________________________________\n",
      "dense_8 (Dense)              (None, 1024)              525312    \n",
      "_________________________________________________________________\n",
      "activation_8 (Activation)    (None, 1024)              0         \n",
      "_________________________________________________________________\n",
      "batch_normalization_9 (Batch (None, 1024)              4096      \n",
      "_________________________________________________________________\n",
      "dense_9 (Dense)              (None, 49)                50225     \n",
      "_________________________________________________________________\n",
      "activation_9 (Activation)    (None, 49)                0         \n",
      "=================================================================\n",
      "Total params: 15,296,369\n",
      "Trainable params: 15,293,297\n",
      "Non-trainable params: 3,072\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "model = build_model_with_vgg16()\n",
    "model.summary()#显示模型结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "loss = 'categorical_crossentropy'\n",
    "optimizer = optimizers.Adam(lr=1e-4)\n",
    "model.compile(loss=loss , optimizer= optimizer, metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "# sgd = optimizers.SGD(lr=0.01, decay=1e-4, momentum=0.9, nesterov=True)\n",
    "optimizer = optimizers.Adam(lr=1e-4)\n",
    "model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['acc'])\n",
    "\n",
    "# change_lr = LearningRateScheduler(scheduler)\n",
    "reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, verbose=1, min_delta=1e-6)\n",
    "\n",
    "save_dir = os.path.join(os.getcwd(), '../../data/trained_model')\n",
    "model_name = 'vgg16_model.{epoch:02d}-{val_acc:.2f}.h5'\n",
    "if not os.path.isdir(save_dir):\n",
    "    os.makedirs(save_dir)\n",
    "filepath = os.path.join(save_dir, model_name)\n",
    "\n",
    "checkpoint = ModelCheckpoint(filepath=filepath,\n",
    "                             monitor='val_acc',\n",
    "                             verbose=1,\n",
    "                             save_best_only=True)\n",
    "\n",
    "#creating early stopping to prevent model from overfitting \n",
    "early_stopping = EarlyStopping(monitor=\"val_acc\", min_delta=0,\n",
    "                                                  patience=8, verbose=1, \n",
    "                                                  mode=\"auto\", baseline=None, \n",
    "                                                  restore_best_weights=True)\n",
    "\n",
    "cbks = [early_stopping, checkpoint, reduce_lr]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_data_gen = ImageDataGenerator(rotation_range=40,\n",
    "    zoom_range=0.2,\n",
    "    width_shift_range=0.2,\n",
    "    height_shift_range=0.2,\n",
    "    shear_range=0.2,\n",
    "    horizontal_flip=True,\n",
    "    fill_mode=\"nearest\")\n",
    "valid_data_gen = ImageDataGenerator()\n",
    "train_data_gen.fit(train_x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/200\n",
      "  2/251 [..............................] - ETA: 10s - loss: 4.7475 - acc: 0.0156WARNING:tensorflow:Callbacks method `on_train_batch_end` is slow compared to the batch time (batch time: 0.0290s vs `on_train_batch_end` time: 0.0578s). Check your callbacks.\n",
      "251/251 [==============================] - ETA: 0s - loss: 4.1820 - acc: 0.0421\n",
      "Epoch 00001: val_acc improved from -inf to 0.01729, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.01-0.02.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 4.1820 - acc: 0.0421 - val_loss: 9.4788 - val_acc: 0.0173\n",
      "Epoch 2/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 3.7634 - acc: 0.0814\n",
      "Epoch 00002: val_acc improved from 0.01729 to 0.02162, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.02-0.02.h5\n",
      "251/251 [==============================] - 24s 95ms/step - loss: 3.7634 - acc: 0.0814 - val_loss: 11.9120 - val_acc: 0.0216\n",
      "Epoch 3/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 3.6116 - acc: 0.1025- ETA: 1s - loss: 3.61\n",
      "Epoch 00003: val_acc did not improve from 0.02162\n",
      "251/251 [==============================] - 23s 92ms/step - loss: 3.6116 - acc: 0.1025 - val_loss: 8.2669 - val_acc: 0.0148\n",
      "Epoch 4/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 3.4421 - acc: 0.1301- ETA\n",
      "Epoch 00004: val_acc improved from 0.02162 to 0.07906, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.04-0.08.h5\n",
      "251/251 [==============================] - 23s 94ms/step - loss: 3.4421 - acc: 0.1301 - val_loss: 4.5920 - val_acc: 0.0791\n",
      "Epoch 5/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 3.3101 - acc: 0.1551\n",
      "Epoch 00005: val_acc did not improve from 0.07906\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 3.3101 - acc: 0.1551 - val_loss: 4.4978 - val_acc: 0.0630\n",
      "Epoch 6/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 3.1398 - acc: 0.1780\n",
      "Epoch 00006: val_acc improved from 0.07906 to 0.09018, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.06-0.09.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 3.1398 - acc: 0.1780 - val_loss: 3.7468 - val_acc: 0.0902\n",
      "Epoch 7/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 2.9479 - acc: 0.2151- ETA: 1s - loss: 2.9\n",
      "Epoch 00007: val_acc improved from 0.09018 to 0.11674, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.07-0.12.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 2.9479 - acc: 0.2151 - val_loss: 4.3405 - val_acc: 0.1167\n",
      "Epoch 8/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 2.8184 - acc: 0.2437\n",
      "Epoch 00008: val_acc improved from 0.11674 to 0.20445, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.08-0.20.h5\n",
      "251/251 [==============================] - 23s 94ms/step - loss: 2.8184 - acc: 0.2437 - val_loss: 3.7574 - val_acc: 0.2044\n",
      "Epoch 9/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 2.6150 - acc: 0.2895\n",
      "Epoch 00009: val_acc improved from 0.20445 to 0.22051, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.09-0.22.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 2.6150 - acc: 0.2895 - val_loss: 3.0891 - val_acc: 0.2205\n",
      "Epoch 10/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 2.4507 - acc: 0.3256\n",
      "Epoch 00010: val_acc improved from 0.22051 to 0.29339, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.10-0.29.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 2.4507 - acc: 0.3256 - val_loss: 2.8567 - val_acc: 0.2934\n",
      "Epoch 11/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 2.2905 - acc: 0.3632- ETA: 2s - lo\n",
      "Epoch 00011: val_acc improved from 0.29339 to 0.33045, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.11-0.33.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 2.2905 - acc: 0.3632 - val_loss: 2.4638 - val_acc: 0.3305\n",
      "Epoch 12/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 2.1133 - acc: 0.4048\n",
      "Epoch 00012: val_acc improved from 0.33045 to 0.36566, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.12-0.37.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 2.1133 - acc: 0.4048 - val_loss: 2.4225 - val_acc: 0.3657\n",
      "Epoch 13/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.9823 - acc: 0.4312- ETA: 0s - loss: 1.9874 - a\n",
      "Epoch 00013: val_acc improved from 0.36566 to 0.37492, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.13-0.37.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 1.9823 - acc: 0.4312 - val_loss: 2.5397 - val_acc: 0.3749\n",
      "Epoch 14/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.8513 - acc: 0.4760\n",
      "Epoch 00014: val_acc improved from 0.37492 to 0.43113, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.14-0.43.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 1.8513 - acc: 0.4760 - val_loss: 2.2548 - val_acc: 0.4311\n",
      "Epoch 15/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.7537 - acc: 0.4993\n",
      "Epoch 00015: val_acc improved from 0.43113 to 0.49846, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.15-0.50.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 1.7537 - acc: 0.4993 - val_loss: 1.7036 - val_acc: 0.4985\n",
      "Epoch 16/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.6186 - acc: 0.5227\n",
      "Epoch 00016: val_acc did not improve from 0.49846\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 1.6186 - acc: 0.5227 - val_loss: 2.3133 - val_acc: 0.4262\n",
      "Epoch 17/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.5398 - acc: 0.5572\n",
      "Epoch 00017: val_acc improved from 0.49846 to 0.50093, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.17-0.50.h5\n",
      "251/251 [==============================] - 23s 94ms/step - loss: 1.5398 - acc: 0.5572 - val_loss: 1.9048 - val_acc: 0.5009\n",
      "Epoch 18/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.4228 - acc: 0.5933\n",
      "Epoch 00018: val_acc improved from 0.50093 to 0.55343, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.18-0.55.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 1.4228 - acc: 0.5933 - val_loss: 1.6493 - val_acc: 0.5534\n",
      "Epoch 19/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.3556 - acc: 0.6029\n",
      "Epoch 00019: val_acc improved from 0.55343 to 0.61211, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.19-0.61.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 1.3556 - acc: 0.6029 - val_loss: 1.3928 - val_acc: 0.6121\n",
      "Epoch 20/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.2548 - acc: 0.6262\n",
      "Epoch 00020: val_acc did not improve from 0.61211\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 1.2548 - acc: 0.6262 - val_loss: 1.5626 - val_acc: 0.5806\n",
      "Epoch 21/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.2177 - acc: 0.6435- ETA: 6s - loss: 1.2266 - - ETA: 5s - loss: 1.\n",
      "Epoch 00021: val_acc did not improve from 0.61211\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 1.2177 - acc: 0.6435 - val_loss: 1.7771 - val_acc: 0.5522\n",
      "Epoch 22/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 1.1369 - acc: 0.6596\n",
      "Epoch 00022: val_acc improved from 0.61211 to 0.63990, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.22-0.64.h5\n",
      "\n",
      "Epoch 00022: ReduceLROnPlateau reducing learning rate to 9.999999747378752e-06.\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 1.1369 - acc: 0.6596 - val_loss: 1.4157 - val_acc: 0.6399\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 23/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.8732 - acc: 0.7395\n",
      "Epoch 00023: val_acc improved from 0.63990 to 0.72761, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.23-0.73.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 0.8732 - acc: 0.7395 - val_loss: 0.9353 - val_acc: 0.7276\n",
      "Epoch 24/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.7859 - acc: 0.7668- ETA: 2s \n",
      "Epoch 00024: val_acc did not improve from 0.72761\n",
      "251/251 [==============================] - 23s 94ms/step - loss: 0.7859 - acc: 0.7668 - val_loss: 0.9331 - val_acc: 0.7196\n",
      "Epoch 25/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.7561 - acc: 0.7790\n",
      "Epoch 00025: val_acc improved from 0.72761 to 0.73996, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.25-0.74.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 0.7561 - acc: 0.7790 - val_loss: 0.8730 - val_acc: 0.7400\n",
      "Epoch 26/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.7157 - acc: 0.7871\n",
      "Epoch 00026: val_acc improved from 0.73996 to 0.74058, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.26-0.74.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 0.7157 - acc: 0.7871 - val_loss: 0.8948 - val_acc: 0.7406\n",
      "Epoch 27/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.7012 - acc: 0.7923\n",
      "Epoch 00027: val_acc improved from 0.74058 to 0.74861, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.27-0.75.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 0.7012 - acc: 0.7923 - val_loss: 0.8592 - val_acc: 0.7486\n",
      "Epoch 28/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.6931 - acc: 0.7897\n",
      "Epoch 00028: val_acc did not improve from 0.74861\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.6931 - acc: 0.7897 - val_loss: 0.8591 - val_acc: 0.7468\n",
      "Epoch 29/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.6579 - acc: 0.8028\n",
      "Epoch 00029: val_acc improved from 0.74861 to 0.75602, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.29-0.76.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 0.6579 - acc: 0.8028 - val_loss: 0.8242 - val_acc: 0.7560\n",
      "Epoch 30/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.6413 - acc: 0.8078\n",
      "Epoch 00030: val_acc improved from 0.75602 to 0.75911, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.30-0.76.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 0.6413 - acc: 0.8078 - val_loss: 0.8347 - val_acc: 0.7591\n",
      "Epoch 31/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.6227 - acc: 0.8128\n",
      "Epoch 00031: val_acc did not improve from 0.75911\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.6227 - acc: 0.8128 - val_loss: 0.8229 - val_acc: 0.7579\n",
      "Epoch 32/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.6073 - acc: 0.8160\n",
      "Epoch 00032: val_acc improved from 0.75911 to 0.75973, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.32-0.76.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 0.6073 - acc: 0.8160 - val_loss: 0.8357 - val_acc: 0.7597\n",
      "Epoch 33/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.6058 - acc: 0.8164- ETA: 2s - los\n",
      "Epoch 00033: val_acc did not improve from 0.75973\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.6058 - acc: 0.8164 - val_loss: 0.8296 - val_acc: 0.7554\n",
      "Epoch 34/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.5781 - acc: 0.8223\n",
      "Epoch 00034: val_acc did not improve from 0.75973\n",
      "\n",
      "Epoch 00034: ReduceLROnPlateau reducing learning rate to 9.999999747378752e-07.\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.5781 - acc: 0.8223 - val_loss: 0.8297 - val_acc: 0.7573\n",
      "Epoch 35/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.5703 - acc: 0.8278\n",
      "Epoch 00035: val_acc improved from 0.75973 to 0.76961, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.35-0.77.h5\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 0.5703 - acc: 0.8278 - val_loss: 0.8120 - val_acc: 0.7696\n",
      "Epoch 36/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.5545 - acc: 0.8360\n",
      "Epoch 00036: val_acc did not improve from 0.76961\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.5545 - acc: 0.8360 - val_loss: 0.8042 - val_acc: 0.7690\n",
      "Epoch 37/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.5522 - acc: 0.8320\n",
      "Epoch 00037: val_acc improved from 0.76961 to 0.77764, saving model to D:\\Projects\\PycharmProjects\\Grad_\\runners\\train\\../../data/trained_model\\vgg16_model.37-0.78.h5\n",
      "251/251 [==============================] - 23s 94ms/step - loss: 0.5522 - acc: 0.8320 - val_loss: 0.7947 - val_acc: 0.7776\n",
      "Epoch 38/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.5454 - acc: 0.8386- ETA: 1s - loss: 0.\n",
      "Epoch 00038: val_acc did not improve from 0.77764\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.5454 - acc: 0.8386 - val_loss: 0.7929 - val_acc: 0.7727\n",
      "Epoch 39/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.5557 - acc: 0.8289\n",
      "Epoch 00039: val_acc did not improve from 0.77764\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.5557 - acc: 0.8289 - val_loss: 0.8010 - val_acc: 0.7684\n",
      "Epoch 40/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.5434 - acc: 0.8360\n",
      "Epoch 00040: val_acc did not improve from 0.77764\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.5434 - acc: 0.8360 - val_loss: 0.8076 - val_acc: 0.7665\n",
      "Epoch 41/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.5457 - acc: 0.8360\n",
      "Epoch 00041: val_acc did not improve from 0.77764\n",
      "\n",
      "Epoch 00041: ReduceLROnPlateau reducing learning rate to 9.999999974752428e-08.\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.5457 - acc: 0.8360 - val_loss: 0.8018 - val_acc: 0.7678\n",
      "Epoch 42/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.5404 - acc: 0.8382\n",
      "Epoch 00042: val_acc did not improve from 0.77764\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.5404 - acc: 0.8382 - val_loss: 0.7983 - val_acc: 0.7678\n",
      "Epoch 43/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.5328 - acc: 0.8384\n",
      "Epoch 00043: val_acc did not improve from 0.77764\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.5328 - acc: 0.8384 - val_loss: 0.7978 - val_acc: 0.7671\n",
      "Epoch 44/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.5434 - acc: 0.8380\n",
      "Epoch 00044: val_acc did not improve from 0.77764\n",
      "\n",
      "Epoch 00044: ReduceLROnPlateau reducing learning rate to 1.0000000116860975e-08.\n",
      "251/251 [==============================] - 23s 93ms/step - loss: 0.5434 - acc: 0.8380 - val_loss: 0.7958 - val_acc: 0.7678\n",
      "Epoch 45/200\n",
      "251/251 [==============================] - ETA: 0s - loss: 0.5370 - acc: 0.8379Restoring model weights from the end of the best epoch.\n",
      "\n",
      "Epoch 00045: val_acc did not improve from 0.77764\n",
      "251/251 [==============================] - 24s 94ms/step - loss: 0.5370 - acc: 0.8379 - val_loss: 0.7971 - val_acc: 0.7690\n",
      "Epoch 00045: early stopping\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tensorflow.python.keras.callbacks.History at 0x2947e3e20f0>"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "batch_size = 32\n",
    "epochs = 200\n",
    "iterations = 251\n",
    "model.fit(train_data_gen.flow(train_x, train_y, batch_size=batch_size),\n",
    "                    steps_per_epoch=iterations,\n",
    "                    epochs=epochs,\n",
    "                    callbacks=cbks,\n",
    "                    validation_data=valid_data_gen.flow(test_x, test_y, batch_size=batch_size))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sgd = optimizers.SGD(lr=0.1, momentum=0.9, nesterov=True)\n",
    "model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['acc'])\n",
    "\n",
    "change_lr = LearningRateScheduler(scheduler)\n",
    "\n",
    "save_dir = os.path.join(os.getcwd(), 'data/trained_model')\n",
    "model_name = 'vgg19_model.{epoch:02d}-{val_acc:.2f}.h5'\n",
    "if not os.path.isdir(save_dir):\n",
    "    os.makedirs(save_dir)\n",
    "filepath = os.path.join(save_dir, model_name)\n",
    "\n",
    "checkpoint = ModelCheckpoint(filepath=filepath,\n",
    "                             monitor='val_acc',\n",
    "                             verbose=1,\n",
    "                             save_best_only=True)\n",
    "\n",
    "#creating early stopping to prevent model from overfitting \n",
    "early_stopping = EarlyStopping(monitor=\"val_acc\", min_delta=0,\n",
    "                                                  patience=10, verbose=1, \n",
    "                                                  mode=\"auto\", baseline=None, \n",
    "                                                  restore_best_weights=True)\n",
    "\n",
    "cbks = [early_stopping, checkpoint, change_lr]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def scheduler(epoch):\n",
    "    if epoch < 80:\n",
    "        return 0.01\n",
    "    if epoch < 160:\n",
    "        return 0.005\n",
    "    return 0.001"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('Using real-time data augmentation.')\n",
    "# datagen = ImageDataGenerator(horizontal_flip=True,\n",
    "#                              width_shift_range=0.125, height_shift_range=0.125, fill_mode='constant', cval=0.)\n",
    "datagen = ImageDataGenerator()\n",
    "valid_data_gen = ImageDataGenerator()\n",
    "datagen.fit(train_x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_x = []\n",
    "test_x = []\n",
    "train_y = []\n",
    "test_y = []\n",
    "X_data = []\n",
    "Y_data = []\n",
    "input_shape=(224,224,3)#3通道图像数据\n",
    "for lists in f:\n",
    "    img = cv2.imread('../../data/data_set/'+lists[1])\n",
    "    img = cv2.resize(img, (input_shape[0], input_shape[1]))\n",
    "    X_data.append(img)\n",
    "    Y_data.append(int(lists[2]))\n",
    "#     if lists[3] == '1':\n",
    "#         train_x.append(img)\n",
    "#         train_y.append(int(lists[2]))\n",
    "#     else:\n",
    "#         test_x.append(img)\n",
    "#         test_y.append(int(lists[2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_x = np.array(train_x).astype('float32') / 255.\n",
    "train_y = np.array(train_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = load_model('../../data/neural_networks/Car_ResNet50.h5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_x_perdict = model(test_x[:512])\n",
    "test_x_perdict_arg = np.argmax(test_x_perdict, axis=1)\n",
    "test_y_arg = np.argmax(test_y[:512], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import classification_report\n",
    "report = classification_report(test_y_arg,test_x_perdict_arg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      1.00      1.00         1\n",
      "           1       0.89      0.73      0.80        22\n",
      "           2       0.83      1.00      0.91         5\n",
      "           3       0.92      1.00      0.96        36\n",
      "           4       0.92      0.88      0.90        25\n",
      "           5       1.00      1.00      1.00        15\n",
      "           6       1.00      1.00      1.00         3\n",
      "           7       0.94      1.00      0.97        17\n",
      "           8       0.80      0.80      0.80         5\n",
      "           9       0.85      0.89      0.87        65\n",
      "          10       0.78      0.82      0.80        22\n",
      "          12       0.92      0.92      0.92        38\n",
      "          13       1.00      1.00      1.00         5\n",
      "          14       1.00      0.75      0.86         4\n",
      "          15       1.00      1.00      1.00        14\n",
      "          16       1.00      1.00      1.00         5\n",
      "          17       0.91      0.91      0.91        33\n",
      "          18       0.88      0.79      0.83        19\n",
      "          19       1.00      0.75      0.86         4\n",
      "          20       1.00      1.00      1.00         1\n",
      "          21       1.00      1.00      1.00         7\n",
      "          22       0.84      1.00      0.92        27\n",
      "          23       1.00      0.50      0.67         2\n",
      "          24       1.00      1.00      1.00         4\n",
      "          25       0.50      0.50      0.50         2\n",
      "          26       0.93      1.00      0.97        14\n",
      "          27       1.00      0.89      0.94         9\n",
      "          28       1.00      0.75      0.86         4\n",
      "          30       1.00      1.00      1.00         3\n",
      "          31       1.00      1.00      1.00         3\n",
      "          32       1.00      0.67      0.80         3\n",
      "          33       1.00      1.00      1.00         1\n",
      "          34       0.93      0.93      0.93        27\n",
      "          35       1.00      1.00      1.00         1\n",
      "          36       1.00      1.00      1.00         8\n",
      "          37       0.80      1.00      0.89         4\n",
      "          38       0.50      1.00      0.67         1\n",
      "          39       1.00      1.00      1.00         4\n",
      "          40       1.00      1.00      1.00         2\n",
      "          41       1.00      0.67      0.80         6\n",
      "          42       1.00      1.00      1.00         5\n",
      "          43       0.67      0.67      0.67         6\n",
      "          44       1.00      1.00      1.00         2\n",
      "          45       1.00      0.83      0.91        12\n",
      "          46       1.00      0.89      0.94         9\n",
      "          47       0.88      1.00      0.93         7\n",
      "\n",
      "    accuracy                           0.91       512\n",
      "   macro avg       0.93      0.90      0.91       512\n",
      "weighted avg       0.91      0.91      0.91       512\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(report)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "json_config = model.to_json()\n",
    "json_config"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('DenseNet_model.json', 'w') as json_file:\n",
    "    json_file.write(json_config)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "grad",
   "language": "python",
   "name": "grad"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
